<template>
  <div>
    <f-search-form :more='true' @searchHandler='searchHandler'>
      <f-search-form-item label='开始日期' type='range' name='beginDate' />
      <f-search-form-item label='订单编号' name='orderNo' />
      <f-search-form-item label='部门' ref='dept' name='deptId' type='select' :data='api.hyorder.deptList' slot='advanced' valueKey='deptId' label-key='deptName' />
    </f-search-form>
    <f-table ref='table' rowKey='orderDetailsId' :tableColumns='columns' :showSetting='false' :scroll='{x:true}' :multiple='true' :operator='operator' :showMultipleBtn='false' :params='queryParam' :api='api.hyOrderDetails' handler='hyWaybillTab4ListHandler'>
        <span slot='sendType' slot-scope='text, record, index'>
           <a-tag color='#2db7f5' v-if='text==1'>现款</a-tag>
            <a-tag color='#FF0000' v-if='text==2'>预收款</a-tag>
            <a-tag color='#87d068' v-if='text==3'>资保金</a-tag>
            <a-tag color='purple' v-if='text==4'>全额资信</a-tag>
        </span>
      <span slot='sendState' slot-scope='text, record, index'>
            <a-tag color='#87d068' v-if='text==0'>未发货</a-tag>
            <a-tag color='purple' v-if='text==1'>已提单</a-tag>
        </span>
      <span slot='action' slot-scope='text, record'>
          <template>
            <a @click='printView(record)'>预览</a>
             <a-divider type='vertical' />
             <a @click='openPrintModal(record)'>打印</a>
          </template>
        </span>
    </f-table>
    <FlymePrintView ref='preView' />
    <a-modal title='商品标签打印' v-model='printModal' width='500px' height='500px' @cancel='this.printModal=false' @ok='print'>
      打印数量:
      <a-input-number id='inputNumber' style='width:200px' v-model='printCount' :min='1' :max='printCount' />
    </a-modal>
  </div>
</template>
<script>
import api from "../../js/api"
import FlymePrintView from "@/components/Flyme/FlymePrintDesigner/preview"

export default {
  name: "Tab4",
  components: { FlymePrintView },
  data() {
    let me = this
    //操作按钮
    let operator = []
    operator.push({
      name: "批量打印",
      that: this,
      handler: (table, that) => {
        let selectIds = table.selectedRowKeys
        if (selectIds.length > 0) {
          this.batchPrint(selectIds)
        } else {
          that.$message.error("请选择打印数据")
        }
      }
    })
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {},
      activeKey: "0",
      printModal: false,
      orderDetail: null,
      printCount: 1,
      // 表头
      columns: [
        {
          title: "序号",
          width: "60px",
          align: "center",
          customRender: (text, record, index) => index + 1
        },
        {
          title: "出库单号",
          align: "left",
          width: "120px",
          dataIndex: "outOrderNo"
        },
        {
          title: "单据日期",
          align: "left",
          width: "120px",
          dataIndex: "sendDate"
        },
        {
          title: "部门",
          align: "left",
          width: "130px",
          dataIndex: "deptName"
        },
        {
          title: "业务员",
          align: "center",
          width: "100px",
          dataIndex: "saleManName"
        },
        {
          title: "单位名称",
          align: "left",
          ellipsis: true,
          width: "120px",
          dataIndex: "customName"
        },

        {
          title: "商品名称",
          align: "left",
          width: "150px",
          dataIndex: "drugsName"
        },
        {
          title: "规格",
          align: "left",
          ellipsis: true,
          length: 30,
          width: "150px",
          dataIndex: "drugsSpec"
        },
        {
          title: "批号",
          align: "left",
          width: "100px",
          dataIndex: "drugLotNo"
        },
        {
          title: "仓库名称",
          align: "left",
          width: "100px",
          dataIndex: "storeName"
        },

        {
          title: "件数",
          align: "left",
          width: "70px",
          dataIndex: "drugJs"
        }, 
        {
          title: "标签数",
          align: "left",
          width: "70px",
          dataIndex: "drugJsCount"
        }, 
        {
          title: "打印次数",
          align: "left",
          width: "70px",
          dataIndex: "outPrintTag"
        },
        {
          title: "操作",
          align: "center",
          dataIndex: "action",
          width: "150px",
          fixed: "right",
          scopedSlots: { customRender: "action" }
        }
      ],
      operator: operator,
      processImageVisible: false,
      processInstanceId: null
    }
  },
  created() {
  },
  methods: {
    onLoad() {
      this.$refs.table.refresh()
    },
    //打印预览
    printView(order) {
      this.http.post(this, {
        url: api.hyOrderDetails.getOutOrderPrint,
        data: {
          orderDetailsId: order.orderDetailsId
        },
        success: (me, data) => {
          let template = data.template
          let printDate = data.dataList[0]
          let printTemplate = JSON.parse(template)
          this.$refs.preView.printView(printTemplate, printDate, 200)
        }
      })
    },
    //直接打印
    print() {
      this.http.post(this, {
        url: api.hyOrderDetails.getOutOrderPrint,
        data: {
          orderDetailsId: this.orderDetail.orderDetailsId
        },
        success: (me, data) => {
          let template = data.template
          let printDate = data.dataList
          let printTemplate = JSON.parse(template)
          this.$refs.preView.batchPrintByCount({
            title: "商品标签",
            template: printTemplate,
            dataList: printDate,
            printCount:this.printCount,
            printSuccess: (response) => {
              this.updatePrintState(printDate[0])
            }
          })
        }
      })
      this.printModal=false;
    },
    openPrintModal(record) {
      this.orderDetail = record
      this.printModal = true
      this.printCount = record.drugJsCount
    },
    //批量打印
    batchPrint(orderDetailsIds) {
      this.http.confirm(this, {
        content: "确认打印吗?",
        url: api.hyOrderDetails.getOutOrderPrint,
        data: {
          orderDetailsId: orderDetailsIds.join(",")
        },
        success: (me, data) => {
          let template = data.template
          let printDate = data.dataList
          let printTemplate = JSON.parse(template)
          this.$refs.preView.batchPrintByCount({
            template: printTemplate,
            dataList: printDate,
            countKey: "drugJsCount",
            printSuccess: (response) => {
              this.updatePrintState(response)
            }
          })
        }
      })
    },
    updatePrintState(order) {
      this.http.post(this, {
        url: api.hyOrderDetails.updateOutPrintTag,
        data: {
          orderDetailsId: order.orderDetailsId
        },
        success: (me, data) => {
          this.$message.success("打印成功")
          this.onLoad()
        }
      })
    },
    changeHandler(activeKey) {
      this.activeKey = activeKey
      this.$refs.table.refresh(true, { activeKey: this.activeKey })
    },
    processImg(order) {
      this.processInstanceId = order.processInstanceId
      this.processImageVisible = true
    }
  }
}
</script>
